/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CadastrarCliente.java
 *
 * Created on 27/04/2010, 13:47:26
 */

package fronteira;

import controle.CadastrarProdutos_Guilherme;
import entidade.Produto_Guilherme;
import java.awt.Color;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Mateus
 */
public class TelaBuscaProduto extends javax.swing.JInternalFrame {


    CadastrarProdutos_Guilherme cadastro;
    public TelaBuscaProduto() {
        this.getContentPane().setBackground(Color.WHITE);
        initComponents();
        this.carregaProdutos();
    }

    public void carregaProdutos(){

    }

    public String[] selecionaProduto(){
        //retorna o produto selecionado
        return null;
    }

    public void limpaTela(){
        this.tela_nome.setText("");
        this.tela_codigo.setText("");
        this.tela_qtd.setText("0");
        while(this.tela_tabela.getRowCount()>0)
             ((DefaultTableModel)this.tela_tabela.getModel()).removeRow(0);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tela_cancelar = new javax.swing.JButton();
        tela_nome = new javax.swing.JTextField();
        tela_buscar2 = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tela_tabela = new javax.swing.JTable();
        jLabel11 = new javax.swing.JLabel();
        tela_selecionar = new javax.swing.JButton();
        jLabel12 = new javax.swing.JLabel();
        tela_codigo = new javax.swing.JTextField();
        tela_buscar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        tela_qtd = new javax.swing.JTextField();
        tela_info = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createCompoundBorder());
        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setIconifiable(true);
        setTitle("Buscar Produto");
        setMaximumSize(new java.awt.Dimension(810, 603));
        setVisible(true);

        tela_cancelar.setText("Cancelar");
        tela_cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tela_cancelarActionPerformed(evt);
            }
        });

        tela_buscar2.setText("Buscar");
        tela_buscar2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tela_buscar2ActionPerformed(evt);
            }
        });

        jLabel10.setText("Digite o nome do produto a ser buscado:");

        tela_tabela.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Código", "Produto", "Quantidade Disponivel", "Preço"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.Integer.class, java.lang.Double.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane2.setViewportView(tela_tabela);

        jLabel11.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel11.setText("Resultado da Busca:");

        tela_selecionar.setText("Selecionar");
        tela_selecionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tela_selecionarActionPerformed(evt);
            }
        });

        jLabel12.setText("Digite o código do produto a ser buscado:");

        tela_buscar.setText("Buscar");
        tela_buscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tela_buscarActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Quantidade:");

        tela_qtd.setFont(new java.awt.Font("Tahoma", 1, 11));
        tela_qtd.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        tela_qtd.setText("1");

        tela_info.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/32x32/info.png"))); // NOI18N
        tela_info.setText("Informações do Produto");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel11)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(tela_nome, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(tela_buscar, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(167, 167, 167)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel12)
                                    .addGroup(layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(tela_codigo, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(tela_buscar2, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(446, 446, 446)
                                .addComponent(jLabel1)
                                .addGap(49, 49, 49)
                                .addComponent(tela_qtd, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane2)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(tela_info, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(105, 105, 105)
                        .addComponent(tela_cancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(94, 94, 94)
                        .addComponent(tela_selecionar, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tela_nome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tela_buscar)
                            .addComponent(tela_codigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel12)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tela_buscar2)))
                .addGap(18, 18, 18)
                .addComponent(jLabel11)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 268, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tela_qtd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tela_selecionar, javax.swing.GroupLayout.DEFAULT_SIZE, 42, Short.MAX_VALUE)
                    .addComponent(tela_info, javax.swing.GroupLayout.DEFAULT_SIZE, 42, Short.MAX_VALUE)
                    .addComponent(tela_cancelar, javax.swing.GroupLayout.DEFAULT_SIZE, 42, Short.MAX_VALUE))
                .addGap(33, 33, 33))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tela_buscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tela_buscarActionPerformed
        // TODO add your handling code here:
        cadastro = new CadastrarProdutos_Guilherme();

        if(this.tela_nome.getText().equals(""))
            JOptionPane.showMessageDialog(null, "Nome inválido!", "Atenção", JOptionPane.ERROR_MESSAGE);
        else{
            ArrayList<Produto_Guilherme> p  = cadastro.ConsultarProduto(tela_nome.getText());
            for (int i = 0; i < p.size(); i++) {
                tela_tabela.setValueAt(p.get(i).getP_codigo(), i, 0);
                tela_tabela.setValueAt(p.get(i).getP_nome(), i, 1);
                tela_tabela.setValueAt(p.get(i).getP_qtd_EmEstoque(), i, 2);
                tela_tabela.setValueAt(p.get(i).getPreco(), i, 3);

            }
            
            //buscar banco de dados
        }
    }//GEN-LAST:event_tela_buscarActionPerformed

    private void tela_buscar2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tela_buscar2ActionPerformed
        // TODO add your handling code here:
        if(this.tela_codigo.getText().equals(""))
            JOptionPane.showMessageDialog(null, "Código Inválido!", "Atenção", JOptionPane.ERROR_MESSAGE);
        else{
            //buscar banco de dados
        }
    }//GEN-LAST:event_tela_buscar2ActionPerformed

    private void tela_selecionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tela_selecionarActionPerformed
        // TODO add your handling code here:

    }//GEN-LAST:event_tela_selecionarActionPerformed

    private void tela_cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tela_cancelarActionPerformed
        // TODO add your handling code here:
        this.limpaTela();
    }//GEN-LAST:event_tela_cancelarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton tela_buscar;
    private javax.swing.JButton tela_buscar2;
    private javax.swing.JButton tela_cancelar;
    private javax.swing.JTextField tela_codigo;
    private javax.swing.JButton tela_info;
    private javax.swing.JTextField tela_nome;
    private javax.swing.JTextField tela_qtd;
    private javax.swing.JButton tela_selecionar;
    private javax.swing.JTable tela_tabela;
    // End of variables declaration//GEN-END:variables

}
